/**
 *
 */
Ext.form.SelectTree = function(config){

	config.readOnly=true;
    Ext.form.SelectTree.superclass.constructor.call(this, config);
    this.addEvents({
    	'select' : true
    	,'expand' : true
    	,'collapse' : true
    	,'beforeselect':true
    	,'blur':true
    });
};
/**
 * hiddenName 
 * displayField 
 * displayValue 
 * valueField 
 * 
 */
 var paramvalue="";
 var showvalue="";
Ext.extend(Ext.form.SelectTree, Ext.form.TriggerField, {
	hiddenName: undefined
	,displayField : 'text'
	,displayValue : undefined
	,valueField: 'id'
	,listAlign: 'tl-bl?'
	,defaultValue: ''
	,shadow:'sides'
	,listClass: ''
	,url:''
	,listpanel:undefined
	,isOver:'false'
	,isOverTree:'false'
	,rootText :'ROOT'
	,rootId:'0'
	,isCheck:false
	,ui:'Ext.tree.TreeNodeUI'
	,multiple:'false',	
	setValue : function(node){
		var text = node[this.displayField];	
		var value = node[this.valueField];
		
		//-----if text||value == null then return else setValue
		
		if(text == '' || text == undefined || value == '' || value == undefined )
		{
		  return;
		}
		if(this.multiple)//if the selecttree is multiple then execute,controle
		{
		   if(this.isCheck)// if the node is checked then  save the value as +=;if 
		   {
			   if(paramvalue!="")
			   {
			    value=","+value;
			    text=","+text;
			   }
			     paramvalue+=value;
			     showvalue+=text;
			     value=paramvalue;
			     text=showvalue;
		   }
		  
		}
		//--------
		if(this.value==value){
			value=this.defaultValue;
		}
		//------------------------------
		if(this.hiddenField){
    	this.hiddenField.value = value;
        }
		Ext.form.SelectTree.superclass.setValue.call(this, text);
		this.value = value;
		//Ext.get(this.name).dom.value=value;
	},
	getValue : function(){
        if(this.valueField){
            return typeof this.value != 'undefined' ? this.value : '';
        }else{
            return Ext.form.SelectTree.superclass.getValue.call(this);
        }
  },
  onSelect:function(node){
		if(this.fireEvent('beforeselect', node, this)!= false){
			this.setValue(node);
	        //this.collapse();
	        this.fireEvent('select', this, node);
	        this.extraFunc(node);
		}
	},

  extraFunc:function(node){
  var second=document.getElementById('second_ld');
    if(second!=null || second==''){
        getUser(node.id);
    }
  },
  unSelect:function(node){// uncheck 
	
		var text = node[this.displayField];	
		var value = node[this.valueField];
		//do with value
		if(paramvalue.indexOf(value+",")==-1)
		{
		paramvalue =paramvalue.replace(","+value,"");
		}else{
		paramvalue =paramvalue.replace(value+",","");
		}
		node[this.valueField]=paramvalue;
		//do with text
		if(showvalue.indexOf(text+",")==-1)
		{
		showvalue =showvalue.replace(","+text,"");
		}else{
		showvalue =showvalue.replace(text+",","");
		}
		node[this.displayField]=showvalue;
		//set value
		if(this.fireEvent('beforeselect', node, this)!= false){
			this.setValue(node);
	        //this.collapse();
	        this.fireEvent('select', this, node);
		}
		//resume the node
		node[this.displayField]=text;
		node[this.valueField]=value;
	},
	createTree:function(el){

		el.setWidth(this.width);
		el.setHeight(this.height);
		if(this.multiple){
			this.ui = 'Ext.tree.CheckboxNodeUI';
		}
		var tree = new Ext.tree.TreePanel(el,{
	        animate:true,
	        enableDD:true,
	        containerScroll: true, 
			loader: new Ext.tree.CustomUITreeLoader({
				dataUrl:this.url,
				baseAttr: {
					name:this.name?this.name:this.hiddenName,
					uiProvider: this.ui
				}
			})
    	});

	    var root = new Ext.tree.AsyncTreeNode({
	        text: this.rootText,
	        draggable:false,
	        id:this.rootId
	    });
	    tree.setRootNode(root);
    	tree.on('beforeload', function(node){
    		if(node.attributes.src)
    		    {
    			tree.loader.dataUrl = node.attributes.src;
    			}
    	});
    	
	    tree.render();
	    return tree;
	},
	onRender : function(ct, position){
	
		var value = this.value;
		Ext.form.SelectTree.superclass.onRender.call(this, ct, position);
		this.value = value;
		if(this.hiddenName){
            this.hiddenField = this.el.insertSibling({tag:'input', type:'hidden', name: this.hiddenName, id:  (this.hiddenId||this.hiddenName)},
                    'before', true);
            this.hiddenField.value =
                this.hiddenValue !== undefined ? this.hiddenValue :
                this.value !== undefined ? this.value : '';
            this.el.dom.removeAttribute('name');
        }
        var cls = 'x-combo-list';
        this.list = new Ext.Layer({
            shadow: this.shadow, cls: [cls, this.listClass].join(' '), constrain:false
        });
        var objLayer=this.list;
        this.listpanel=objLayer;
     
        this.list.swallowEvent('mousewheel');
        this.innerList = this.list.createChild({cls:cls+'-inner'});
        var objInner=this.innerList;
               // this.listpanel=objInner;
        var tree = this.createTree(this.innerList);

        var treeField = this;
        tree.on('click',function(node,e){
        	if(treeField.multiple)
        	{
        	   treeField.isCheck=false;
	            paramvalue="";
	            showvalue="";
            }else{
              treeField.onSelect(node);
              treeField.onTriggerClick();
            }
			});
		tree.on('check',function(node,checked){
	    	 if(checked)
	    	 {
	    	  treeField.isCheck=true;//
	          treeField.onSelect(node);
	         }else{
	          treeField.isCheck=false;
	          treeField.unSelect(node);
	         }
    	});

        this.tree = tree;

	    var root = new Ext.tree.AsyncTreeNode({
	        text: this.rootText,
	        draggable:false,
	        id:this.rootId
	    });
	    this.onSelect(root);
	},
	onDestroy : function(){
        if(this.list){
            this.list.destroy();
        }
        Ext.form.SelectTree.superclass.onDestroy.call(this);
  },
	isExpanded : function(){
        return this.list.isVisible();
  },
	collapse : function(){
        if(!this.isExpanded()){
            return;
        }
        this.list.hide();
        Ext.get(document).un('mousedown', this.collapseIf, this);
        Ext.get(document).un('mousewheel', this.collapseIf, this);
        this.fireEvent('collapse', this);
  },
	collapseIf : function(e){
        if(!e.within(this.wrap) && !e.within(this.list)){
            this.collapse();
        }
  },
	expand : function(){
        if(this.isExpanded()){
            return;
        }
        this.list.alignTo(this.el, this.listAlign);
        this.list.show();
        Ext.get(document).on('mousedown', this.collapseIf, this);
        Ext.get(document).on('mousewheel', this.collapseIf, this);
        this.fireEvent('expand', this);
  },
  onTriggerClick : function(){
        this.focus();
        if(this.disabled){
            return;
        }
        if(this.isExpanded()){
            this.collapse();
        }else {
            this.expand();
        }
         this.el.focus();
  }
});
